package com.zdd.ehe.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zdd.ehe.entity.News;
import com.zdd.ehe.entity.Pest;
import com.zdd.ehe.entity.ProductPrice;
import com.zdd.ehe.entity.ProvinceProduce;
import com.zdd.ehe.param.TypeParam;
import com.zdd.ehe.result.Result;
import com.zdd.ehe.service.*;
import com.zdd.ehe.vo.NewsVo;
import com.zdd.ehe.vo.ProvincePestVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @version 1.0.0
 * @title: ShowController
 * @author: HFLy
 * @description: 首页大屏
 * @date: 6/3/2023 下午4:08
 */
@Api(tags = "大屏显示")
@RestController
@RequestMapping("/show")
public class ShowController {
    @Autowired
    private ProvinceProduceService provinceProduceService;
    @Autowired
    private PestService pestService;
    @Autowired
    private NewsService newsService;
    @Autowired
    private ProvincePestService provincePestService;
    @Autowired
    private ProductPriceService productPriceService;
    @ApiOperation("全国种植分布")
    @PostMapping("/provinceProduce")
    public Result ProvinceProduce(@RequestBody TypeParam typeParam){
        List<ProvinceProduce> list = provinceProduceService.list(
                new QueryWrapper<ProvinceProduce>()
                        .eq("type",typeParam.getType())
                        .orderByAsc("year"));
        return Result.success("查询成功",list);
    }
    @ApiOperation("全国稻谷病虫害")
    @PostMapping("/pest")
    public Result pest(@RequestBody TypeParam typeParam){
        System.out.println( typeParam.getType());
        List<Pest> list = pestService.list(
                new QueryWrapper<Pest>()
                        .eq("type", typeParam.getType())
                        .orderByAsc("year")
        );
        return Result.success("查询成功",list);
    }
    @ApiOperation("新闻")
    @PostMapping("/news")
    public Result news(@RequestBody TypeParam typeParam){
        List<News> list = newsService.list(new QueryWrapper<News>().eq("type", typeParam.getType()).select("title","url"));
        List<NewsVo> listVo =new ArrayList<>();
        for (News news : list) {
            NewsVo newsVo = new NewsVo();
            BeanUtils.copyProperties(news,newsVo);
            listVo.add(newsVo);
        }
        return Result.success("查询成功",listVo);
    }
    @ApiOperation("病虫害饼图")
    @PostMapping("/provincePest")
    public Result provincePest(@RequestBody TypeParam typeParam){
        List<ProvincePestVo>  listVo= provincePestService.show(typeParam.getType());
        return Result.success("查询成功",listVo);
    }
    @ApiOperation("主要产品价格")
    @PostMapping("/productPrice")
    public Result productPrice(@RequestBody TypeParam typeParam){
        System.out.println(typeParam.getType());
        List<ProductPrice> productPriceList = productPriceService.list(new QueryWrapper<ProductPrice>()
                .eq("type", typeParam.getType())
                .orderByAsc("date")
        );
        return Result.success("查询成功",productPriceList);
    }
}
